package com.example.streamdemo.producer;

import com.example.streamdemo.bean.Person;
import com.example.streamdemo.channel.MyOutputChannel;
import com.example.streamdemo.channel.NewChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @Package: com.example.streamdemo.producer
 * @ClassName: MyMessageProducer
 * @Author: Think
 * @CreateTime: 2021/7/16 8:24
 * @Description:
 */
@Slf4j
@Component
@EnableBinding(NewChannel.class)
public class MyMessageProducer3 {

    @Autowired
    private NewChannel newChannel;

    public void send(Person person){
        log.info("MyMessageProducer3生产者开始发送消息！");
        Message<Person> message1 = MessageBuilder.withPayload(person)
                // 指定ROcketMQ中消息的TAG
                .setHeader(RocketMQHeaders.TAGS, "animal")
                // 自定义header用于consumer监听器进行过滤
                .setHeader("MyTag", "TAG1")
                .build();

        Message<Person> message2 = MessageBuilder.withPayload(person)
                .setHeader(RocketMQHeaders.TAGS, "person")
                .setHeader("MyTag", "TAG2")
                .build();

        Message<Person> message3 = MessageBuilder.withPayload(person)
                .setHeader(RocketMQHeaders.TAGS, "person")
                .setHeader("MyTag", "TAG3")
                .build();
        newChannel.output().send(message1);
        newChannel.output().send(message2);
        newChannel.output().send(message3);
    }

}
